package com.example.myapplication.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication.R;
import com.example.myapplication.entity.UserInfo;

import java.util.List;

/**
 * @author sjh
 * @Description
 * @create 2022/3/1 9:30
 */
public class MyAdapter extends BaseAdapter {

    private List<UserInfo> list;

    private Context context;

    public MyAdapter(List<UserInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //每次滚动都会调用其中的方法
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //看看这是什么球东西
        if (convertView == null) {
            Log.e("TAG", "convertView is null");
        } else {
            Log.e("convertView", convertView.getAccessibilityClassName().toString());    //ViewGroup
        }

        //看看这是什么球东西
        Log.e("parent", parent.getAccessibilityClassName().toString());       //ListView


        //Inflater增压泵   inflate膨胀、充气
        //LayoutInflater作用：将一个xml定义的Layout文件查找出来，渲染成View；     而findViewById是从一个Layout中查找控件，渲染成view
        //获取activity的渲染器，使用渲染器将item布局渲染进ListView
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        }

        TextView textView1 = convertView.findViewById(R.id.item_tv1);
        textView1.setText(list.get(position).getTitle());

        TextView textView2 = convertView.findViewById(R.id.item_tv2);
        textView2.setText(list.get(position).getSubTitle());

        ImageView imageView = convertView.findViewById(R.id.item_img);
        imageView.setImageResource(R.drawable.ic_baseline_airplanemode_active_24);

        //优化：findViewById很费时间，自定义一个ViewHolder用来存储
//        ViewHolder viewHolder;
//        if (convertView == null) {
//            viewHolder = new ViewHolder();
//            convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
//            viewHolder.textView1 = convertView.findViewById(R.id.item_tv1);
//            viewHolder.textView2 = convertView.findViewById(R.id.item_tv2);
//            viewHolder.imageView = convertView.findViewById(R.id.item_img);
//            convertView.setTag(viewHolder);   //转存
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//
//        viewHolder.textView1.setText(list.get(position).getTitle());
//        viewHolder.textView2.setText(list.get(position).getSubTitle());
//        viewHolder.imageView.setImageResource(R.drawable.ic_baseline_airplanemode_active_24);

        return convertView;
    }

    private static class ViewHolder {
        TextView textView1;
        TextView textView2;
        ImageView imageView;
    }

}
